local rank = fk.CreateSkill{
    name = "glory_days__rank"
}
Fk:loadTranslationTable{
    ["glory_days__rank"] = "官阶展示",
    [":glory_days__rank"] = "若不禁用该武将，斗地主、2v2、身份模式对局结束会进入官阶界面。<br>禁用后，帅点也会正常计算。",
    ["#glory_days_rank_log"] = "%from 每日签到成功，获得500帅点",
}

local gdU = require "packages/glory_days/utility"

local ranks = {"dajiangjun","biaoqijiangjun","cheqijiangjun","weijiangjun",
    "guoduhu","guohujun","shangjiangjun","jiangjun",
    "pianjiangjun","langjiang","xiaowei","xiaozu"
}
local rankNums = {350000, 250000, 200000, 160000, 130000, 80000, 60000, 52000, 12000, 8000, 4000, 0}

rank:addEffect(fk.GameStart, {
    priority = 0.001,
    global = true,
    can_refresh = function(self, event, target, player, data)
        return target == player
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local cpRanks = {}
        for _, cp in ipairs(room.players) do
            if cp.id > 0 then
                local cpRank = gdU.getOfficialLevel(room, cp)
                table.insert(cpRanks, {cp, cpRank})
                --room:setEmotion(cp, "./packages/glory_days/image/anim/guanjie/"+cpRank)
            else
                local cpRank = table.random({
                    "biaoqijiangjun","cheqijiangjun","weijiangjun",
                    "guoduhu","guohujun","shangjiangjun","jiangjun",
                    "pianjiangjun","langjiang","xiaowei","xiaozu"
                })
                table.insert(cpRanks, {cp, cpRank})
                --room:setEmotion(cp, "./packages/glory_days/image/anim/guanjie/"+cpRank)
            end
            --[[room:doAnimate("SuperLightBox", {
                path = "packages/glory_days/qml/OfficialRank.qml",
                data = {
                    count = count
                }
            }, { cp })]]--
        end
        for _, cpRankData in ipairs(cpRanks) do
            --room:setPlayerMark(cpRankData[1], "@!glory_days_"+cpRankData[2], 1)
            room:setEmotion(cpRankData[1], "./packages/glory_days/image/anim/guanjie/"+cpRankData[2])
        end
        room:delay(1800)
    end,
})

--游戏结束时开始结算帅点
rank:addEffect(fk.GameFinished, {
    priority = -1,
    global = true,
    is_delay_effect = true,
    can_trigger = function (self, event, target, player, data)
        local room = player.room
        local iswhitemode = false
        for _, mode in ipairs({ "1v2_mode","2v2_mode","role_mode" }) do
            if room:isGameMode(mode) then
                iswhitemode = true
                break
            end
        end
        return (not room:getBanner("has_get")) and iswhitemode
    end,
    on_cost = function (self, event, target, player, data)
        local room=player.room
        room:setBanner("has_get",1)
        local white_mode = { "1v2_mode","2v2_mode","role_mode" }
        local score = {20,40,60}
        local winScore = {5,10,20}
        local win = data:split("+")
        local hasAI = #table.filter(room.players, function (p)
            return p.id < 1
        end) > 0
        local winners = table.filter(room.players, function(p)
            return table.contains(win, p.role) and p.id > 0 and not table.contains(room.observers, p)
        end)
        local losers = table.filter(room.players,function(p)
            return (not table.contains(win,p.role)) and p.id > 0 and not table.contains(room.observers, p)
        end)
        local num = 0
        for index, mode in ipairs(white_mode) do
            if room:isGameMode(mode) then
                if room:isGameMode("role_mode") then
                    num = math.min(3, #room.players - 2)
                else
                    num = index
                end
                break
            end
        end

        if num > 0 and os.time() - room.start_time >= 120 then
            for _, cp in ipairs(winners) do
                local shauidian = score[num] + winScore[num]
                if hasAI then shauidian = math.floor(shauidian/2) end
                gdU.changePlayerRank(room, cp, shauidian)
            end
            for _, cp in ipairs(losers) do
                local shauidian = score[num]
                if hasAI then shauidian = math.floor(shauidian/2) end
                gdU.changePlayerRank(room, cp, shauidian)
            end

            room:delay(2000)
        end

        if not table.contains(Fk:currentRoom().disabled_generals, "glory_days__officialRank") then
            for _, cp in ipairs(room.players) do
                local count = gdU.getOfficialRank(room, cp)
                room:doAnimate("SuperLightBox", {
                    path = "packages/glory_days/qml/OfficialRank.qml",
                    data = {
                        count = count
                    }
                }, { cp })
            end
        end

        return false
    end,
})

return rank